package com.zxy.demo.java;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * Hello world!
 */
public class App2 {
  public static void main(String[] args) {

    List<Person> personList = new ArrayList<>();
    personList.add(new Person(1L, 1));
    personList.add(new Person(2L, 2));
    personList.add(new Person(3L, 2));

    Map<Object, Boolean> seen = new ConcurrentHashMap<>();

    personList = personList.stream().filter(x -> seen.putIfAbsent(x.age, Boolean.TRUE) == null)
        .collect(Collectors.toList());
    System.out.println(personList);
  }

  public static class Person {
    public Long id;
    public Integer age;

    public Person(Long id, Integer age) {
      this.id = id;
      this.age = age;
    }

    @Override
    public String toString() {
      final StringBuilder sb = new StringBuilder("Person{");
      sb.append("id=").append(id);
      sb.append(", age=").append(age);
      sb.append('}');
      return sb.toString();
    }
  }
}
